"use strict";
/* Controllers */
app.controller("userBindController", ['$scope', '$location', '$rootScope', '$routeParams', 'userBindService', function ($scope, $location, $rootScope, $routeParams, userBindService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.ID = $routeParams.ID; // 如果ID!=0为更新

    if(vm.p.ID!=0) {
        var users = {
            ID: vm.p.ID
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                vm.p = response.data;
            } else {
                alert(response.message);
            }
        };
        userBindService.getUser(users, procFn, errorFn);
    }

    var errorFn = function (result) {
        alert(result);
    };
    $scope.bindUser = function () {
        var vm = $scope.vm;

        var users = {
            ID: vm.p.ID,
            userID: vm.p.userID,
            loginName: vm.p.loginName,
            userName: vm.p.userName,
            customerID: vm.p.customerID,
            customerName: vm.p.customerName
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                //window.location = "#/income/user_list/0";
                alert("执行成功！")
            } else {
                alert(response.msg);
            }
        };
        userBindService.bindUser(users, procFn, errorFn);
    };

}]);


/* Services */
app.service('userBindService', ['ajaxService', function (ajaxService) {

    this.bindUser = function (users, successFunction, errorFunction) {
        ajaxService.AjaxPost(users, "/star/api/basic-service/v1/user/bindUser.do", successFunction, errorFunction);
    };

    this.getUser = function (users, successFunction, errorFunction) {
        ajaxService.AjaxPost(users, "/star/api/basic-service/v1/user/getUser.do", successFunction, errorFunction);
    };

}]);

